********************************************************************************
*                                                                              *
*                      CREATE ANNUAL EARNINGS DATA                             *
*                                                                              *
********************************************************************************

cd "H:\"

global minyear = 2003
global maxyear = 2020

forvalues year = $minyear(1)2015 {
	if `year' < 2009 | `year' == 2010 | `year' == 2013 | `year' == 2014 {
		use "G:\InkomenBestedingen\INTEGRAAL PERSOONLIJK INKOMEN\\`year'\geconverteerde data\PERSOONINK`year'TABV3.dta", clear
	}
	if `year' == 2009 {
		use "G:\InkomenBestedingen\INTEGRAAL PERSOONLIJK INKOMEN\\`year'\geconverteerde data\PERSOONINK`year'TABV2.dta", clear
	}
	if `year' == 2011 {
		use "G:\InkomenBestedingen\INTEGRAAL PERSOONLIJK INKOMEN\\`year'\geconverteerde data\131107 Integraal Persoonlijk Inkomen `year'V2.dta", clear
	}
	if `year' == 2012 {
		use "G:\InkomenBestedingen\INTEGRAAL PERSOONLIJK INKOMEN\\`year'\geconverteerde data\141124 Integraal Persoonlijk Inkomen `year'V2.dta", clear
	}	
	if `year' == 2015 {
		use "G:\InkomenBestedingen\INTEGRAAL PERSOONLIJK INKOMEN\\`year'\geconverteerde data\PERSOONINK`year'TABV1.dta", clear
	}
	
	rename *, lower
	
	if `year' < 2011 | `year' > 2012 {
		keep rinpersoon persbrut persink seccoal1 poshhk
	}
	if `year' == 2011 | `year' == 2012 {
		keep rinpersoon vrlposhhk vrlseccoal1 vrlpersbrut vrlpersink
		rename (vrlposhhk vrlseccoal1 vrlpersbrut vrlpersink) (poshhk seccoal1 persbrut persink)
	}
	
	g year = `year'
	g earnings_gross = persbrut
	g earnings = persink
	replace earnings_gross = 0 if earnings_gross <0
	replace earnings = 0 if earnings <0
	g jobtype_employee = seccoal1 == "001" | seccoal1 == "002"
	//g jobtype_civilserv = seccoal1 == "002"
	g jobtype_selfempl = seccoal1 == "003" | seccoal1 == "004" | seccoal1 == "005"
	g jobtype_unempl = seccoal1 == "006" 
	g jobtype_inactive = seccoal1 == "007" | seccoal1 == "008"
	g jobtype_retired = seccoal1 == "010" | seccoal1 == "011" 
	g jobtype_student = seccoal1 == "012" 
	g jobtype_other = seccoal1 == "013" | seccoal1 == "014" 
	drop if seccoal1=="099"
	g hh_single = poshhk == "001"
	g hh_couple = poshhk == "002" | poshhk == "003" | poshhk == "004"
	g hh_child = poshhk == "005" | poshhk == "006"
	g hh_other = poshhk == "007" | poshhk == "099"
	drop if hh_child==1
	drop seccoal1 persbrut persink poshhk hh_child
	compress
	duplicates drop rinpersoon, force
	do "H:\Robots and Workers\Do-files\Variable labels.do"
	save "H:\Robots and Workers\Data\Earnings_`year'.dta",replace
}

forvalues year = 2011(1)$maxyear {
	if `year' <= 2015 | `year' == 2018 | `year' == 2019 {
		use "G:\InkomenBestedingen\INPATAB\geconverteerde data\INPA`year'TABV2.DTA", clear
	}	
	if `year' == 2016 | `year' == 2017 {
		use "G:\InkomenBestedingen\INPATAB\geconverteerde data\INPA`year'TABV3.DTA", clear
	}
	if `year' == 2020 {
		use "G:\InkomenBestedingen\INPATAB\geconverteerde data\INPA`year'TABV1.DTA", clear
	}
	rename *, lower
	keep rinpersoon inpposhhk inpsecj inppersbrut inppersink

	g year = `year'
	g earnings_gross = inppersbrut
	g earnings = inppersink
	replace earnings_gross = 0 if earnings_gross <0
	replace earnings = 0 if earnings <0	
	g jobtype_employee = inpsecj == "11"
	g jobtype_selfempl = inpsecj == "12" | inpsecj == "13" | inpsecj == "14"
	g jobtype_unempl = inpsecj == "21" 
	g jobtype_inactive = inpsecj == "22" | inpsecj == "23" | inpsecj == "24"
	g jobtype_retired = inpsecj == "25" 
	g jobtype_student = inpsecj == "26" |  inpsecj == "27" 
	g jobtype_other = inpsecj == "15" | inpsecj == "32" 
	drop if inpsecj == "99" 
	g hh_single = inpposhhk == "1"
	g hh_couple = inpposhhk == "2" | inpposhhk == "3" | inpposhhk == "4"
	g hh_child = inpposhhk == "5" | inpposhhk == "6"
	g hh_other = inpposhhk == "7" | inpposhhk == "9"
	drop if hh_child==1
	drop inpposhhk inpsecj inppersbrut inppersink hh_child
	compress
	duplicates drop rinpersoon, force
	do "H:\Robots and Workers\Do-files\Variable labels.do"
	
	if `year' <= 2015 {
		rename (earnings_gross earnings) (earnings_gross_ earnings_)	
		merge 1:1 rinpersoon year using "H:\Robots and Workers\Data\Earnings_`year'.dta", nogen update keep(1 2 3 4 5)
		replace earnings = max(earnings, earnings_)
		replace earnings_gross = max(earnings_gross, earnings_gross_)
		drop earnings_gross_ earnings_
	}
	
	save "H:\Robots and Workers\Data\Earnings_`year'.dta",replace
}

forvalues year = $minyear(1)$maxyear {
	qui use "H:\Robots and Workers\Data\Earnings_`year'.dta", clear
	su year earnings_gross earnings jobtype_employee jobtype_unempl hh_single
}
	